package com.phone.modules.rms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.phone.common.api.CommonPage;
import com.phone.common.api.CommonResult;
import com.phone.modules.rms.dto.RmsQueryDTO;
import com.phone.modules.rms.model.RmsBatteryLife;
import com.phone.modules.rms.service.RmsBatteryLifeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 电池续航排行表 前端控制器
 * </p>
 *
 * @author WangHao
 * @since 2022-04-17
 */
@CrossOrigin(origins="*",maxAge=3600)
@RestController
@RequestMapping("/batteryLife")
public class RmsBatteryLifeController {

    @Autowired
    RmsBatteryLifeService batteryLifeService;

    @ApiOperation("前台展示-检索电池续航信息")
    @RequestMapping(value = "/searchList", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult searchList(RmsQueryDTO batteryQueryDTO) {
        Page page = batteryLifeService.searchList(batteryQueryDTO);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @ApiOperation(value = "分页获取电池续航列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getList(@RequestParam(value = "keyword", required = false) String keyword,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page page = batteryLifeService.listBatteryLife(keyword, pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @ApiOperation(value = "添加电池续航")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody RmsBatteryLife batteryLife) {
        boolean result = batteryLifeService.saveBatteryLife(batteryLife);
        if (result) {
            return CommonResult.success(result);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "根据编号查询电池续航信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getItem(@PathVariable("id") Long id) {
        return CommonResult.success(batteryLifeService.getById(id));
    }

    @ApiOperation(value = "更新电池续航")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable("id") Long id,
                               @RequestBody RmsBatteryLife batteryLife) {
        boolean result = batteryLifeService.updateBatteryLife(id, batteryLife);
        if (result) {
            return CommonResult.success(result);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "批量删除电池续航信息")
    @RequestMapping(value = "/delete/batch", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult deleteBatch(@RequestParam("ids") List<Long> ids) {
        boolean result = batteryLifeService.deleteBatteryLife(ids);
        if (result) {
            return CommonResult.success(result);
        } else {
            return CommonResult.failed();
        }
    }
}

